package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.device.DevicePageQuery;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.guokang.GuoKangClientQuery;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.csunsleep.DmcCsunSleepVo;
import com.csun.cmny.provider.model.vo.guokang.GuoKangClientVo;
import com.csun.cmny.provider.model.vo.guokang.GuoKangCsunSleepVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcGuoKangClientService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-12-09 17:27
 * @Description:
 */
@RestController
@RequestMapping(value = "/guokang", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcGuoKangClientController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcGuoKangClientService dmcGuoKangClientService;

    /**
     * 保存
     *
     * @param dmcGuoKangClient
     *
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@RequestBody DmcGuoKangClient dmcGuoKangClient) {

        Integer result = dmcGuoKangClientService.save(dmcGuoKangClient, getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * 根据Id删除
     *
     * @param id
     *
     * @return
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcGuoKangClientService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * 查询列表 - 分页
     *
     * @param guoKangClientQuery
     *
     * @return
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody GuoKangClientQuery guoKangClientQuery) {

        List<GuoKangClientVo> guoKangClientVoList = dmcGuoKangClientService.queryListWithPage(guoKangClientQuery);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(guoKangClientVoList));
    }

    /**
     * 绑定
     */
    @PostMapping(value = "/bind")
    public Wrapper bind(@RequestBody DmcGuoKangDevice dmcGuoKangDevice) {

        Integer result = dmcGuoKangClientService.bind(dmcGuoKangDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 解绑
     */
    @PostMapping(value = "/untied")
    public Wrapper untied(@RequestBody DmcGuoKangDevice dmcGuoKangDevice) {

        Integer result = dmcGuoKangClientService.untied(dmcGuoKangDevice);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 查询未绑定的床垫
     */
    @PostMapping(value = "/queryNotBindCsunSleepList")
    public Wrapper queryNotBindCsunSleepList() {

        List<GuoKangCsunSleepVo> guoKangCsunSleepVoList = dmcGuoKangClientService.queryNotBindCsunSleepList();

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), guoKangCsunSleepVoList);
    }

}
